package com.fita.webpay.processor.impl;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.demo2do.core.wechat.entity.pay.PayConfig;
import com.fita.webpay.entity.PaymentContext;
import com.fita.webpay.entity.config.HandpayConfig;
import com.fita.webpay.entity.context.HandpayContext;
import com.fita.webpay.entity.prepare.PrepareResponse;
import com.fita.webpay.processor.PaymentProcessor;

/**
 * 
 * @author mohanlan
 *
 */
@Component("handpayProcessor")
public class HandpayProcessor implements PaymentProcessor {
    
    private static final Logger logger = LoggerFactory.getLogger(HandpayProcessor.class);
    
    private HandpayConfig handpayConfig;
    
    @Value("#{payment['handpay.appKey']}")
    private String appKey;
    
    /**
    *
    * @param appId
    * @param partnerId
    * @param notifyUrl
    * @param spbillCreateIp
    */
   @Autowired
   public void initHandpayConfig(@Value("#{payment['handpay.appId']}") String appId,
                                 @Value("#{payment['handpay.appNum']}") String appNum,
                                 @Value("#{payment['handpay.partnerId']}") String partnerId,
                                 @Value("#{payment['handpay.notifyUrl']}") String notifyUrl){
       
       // init static configurations
       this.handpayConfig = new HandpayConfig(appId, appNum, partnerId, notifyUrl);
   }
   
    /*
     * (non-Javadoc)
     * @see com.fita.webpay.processor.PaymentProcessor#prepare(com.fita.webpay.entity.PaymentContext)
     */
    public PrepareResponse prepare(PaymentContext paymentContext) {
        
        PrepareResponse prepareResponse = new PrepareResponse();

        try {
            
            // 1. generate handpayContext & validate
            HandpayContext handpayContext = new HandpayContext(handpayConfig);
            handpayContext.init(paymentContext);
            
            if(handpayContext.isValid()) {
                logger.error("#prepare - An error occurs when valid handpayContext.");
                prepareResponse.fail().message("创建瀚银支付环境失败!");
            }
            
            // 2. make sign
            String signString = handpayContext.generateSignString().concat("|").concat(appKey).replace("null", "");
            String sign = DigestUtils.md5Hex(signString);
            handpayContext.sign(sign);
            
            prepareResponse.success().data(handpayContext);
            
        } catch(Exception e) {
            
            logger.error("#prepare - An error occurs when creating handpay context.", e);
            
            prepareResponse.fail().message("创建瀚银支付环境失败!");
            
        }
        
        return prepareResponse;
    }

    /*
     * (non-Javadoc)
     * @see com.fita.webpay.processor.PaymentProcessor#validate(com.demo2do.core.wechat.entity.pay.PayConfig)
     */
    public boolean validate(PayConfig payConfig) {
        return payConfig.match(handpayConfig.getAppid(), handpayConfig.getSellerId());
    }
    
}
